package com.twq.collection.framework.set

/**
 * Created by tangweiqun on 2017/11/15.
 */
object SetTest {
  def main(args: Array[String]): Unit = {
    //Set 提供了：
    // 1、高效的检查集合是否包含某元素的实现
    // 2、元素去重的功能

    //set的另一个特性: 可以用作过滤函数
    (0 to 100) filter Set(2, 3, 4) //Set(2, 3, 4)就是一个过滤函数

    //所有的方法：
    val fruit = Set("apple", "orange", "peach", "banana") //默认实现是scala.collection.immutable.Set
    //Tests
    fruit.contains("apple")
    fruit("apple") //和fruit.contains("apple")功能是一样的
    fruit.subsetOf(Set("apple", "orange", "peach", "banana", "tes"))

    //Additions
    fruit + "pear"
    fruit + ("pear", "grape")
    fruit ++ Seq("grape")

    //Removals
    fruit - "pear"
    fruit - ("pear", "grape")
    fruit -- Seq("grape")
    fruit.empty

    //Binary Operations
    fruit & Set("orange")
    fruit.intersect(Set("orange"))

    fruit | Set("orange")
    fruit.union(Set("orange"))

    fruit &~ Set("orange")
    fruit.diff(Set("orange"))

    //可变Set新增的方法
    val mutableSet = scala.collection.mutable.Set(1, 2, 3, 4, 1)
    //Additions
    mutableSet += 99
    mutableSet += (88, 109)
    mutableSet ++= Seq(22, 11, 23)
    mutableSet.add(999)

    //Removals
    mutableSet -= 99
    mutableSet -= (88, 109)
    mutableSet --= Seq(22, 11, 23)
    mutableSet.remove(999)
    mutableSet.retain(_ > 10) //保留大于10的元素
    mutableSet.clear()

    //Update
    mutableSet(999999) = true
    mutableSet.update(999999, true) //两者功能是一样的

    //Cloning
    mutableSet.clone()
  }
}


